package cn.b.sky.mes.qm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import cn.b.sky.mes.qm.service.ResponsibilityTypeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.mes.qm.dao.ResponsibilityTypeDao;
import cn.b.sky.mes.qm.model.ResponsibilityType;
import cn.b.sky.mes.qm.model.condition.ResponsibilityTypeCondition;

@Service("responsibilityTypeService")
public class ResponsibilityTypeServiceImpl implements ResponsibilityTypeService {
  
  private static final Logger LOG = LoggerFactory.getLogger(ResponsibilityTypeServiceImpl.class);

  @Resource
  ResponsibilityTypeDao responsibilityTypeDao;

  @Override
  public List<ResponsibilityType> selectResponsibilityTypeListByCondition(ResponsibilityTypeCondition condition) {
    if (condition == null) {
      condition = new ResponsibilityTypeCondition();
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("CreateDateTime");
    }
    return responsibilityTypeDao.selectResponsibilityTypeListByCondition(condition);
  }

  @Override
  public int selectResponsibilityTypeCountByCondition(ResponsibilityTypeCondition condition) {
    if (condition == null) {
      condition = new ResponsibilityTypeCondition();
    }
    return responsibilityTypeDao.selectResponsibilityTypeCountByCondition(condition);
  }

  @Override
  public ResponsibilityType getResponsibilityType(String siteCode, String responsibilityTypeCode) {
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(responsibilityTypeCode)) {
      return null;
    }
    return responsibilityTypeDao.getResponsibilityType(siteCode, responsibilityTypeCode);
  }

  @Override
  public void insertResponsibilityType(ResponsibilityType responsibilityType) {
    if (StringUtils.isBlank(responsibilityType.getSiteCode())
        || StringUtils.isBlank(responsibilityType.getResponsibilityTypeCode())) {
      return;
    }
    responsibilityTypeDao.insertResponsibilityType(responsibilityType);
  }

  @Override
  public void updateResponsibilityType(ResponsibilityType responsibilityType) {
    if (StringUtils.isBlank(responsibilityType.getSiteCode())
        || StringUtils.isBlank(responsibilityType.getResponsibilityTypeCode())) {
      return;
    }
    responsibilityTypeDao.updateResponsibilityType(responsibilityType);

  }

  @Override
  public BatchJobReport deleteResponsibilityType(String siteCode, String responsibilityTypeCode) {
    String languageCode = SecurityContextUtil.getLanguageCode();
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(responsibilityTypeCode)) {
      return new BatchJobReport("", "siteCode or responsibilityTypeCode is null", false, languageCode, SecurityContextUtil.getSiteCode());
    }
    String message = responsibilityTypeCode;
    boolean success = true;
    String cause = "";

    try {
      responsibilityTypeDao.deleteResponsibilityType(siteCode, responsibilityTypeCode);
    } catch (Exception e) {
      LOG.error("{}", e);
      success = false;
      String errorMsg = e.getMessage();
      if (DatabaseUtils.isConstraintsError(errorMsg)) {
        //外键删除已存在提示
        errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),languageCode, "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),languageCode, "notDelete.M");
      }
      cause = errorMsg;
    }
    return new BatchJobReport(message, cause, success, languageCode, SecurityContextUtil.getSiteCode());
  }

  @Override
  public List<ResponsibilityType> selectResponsibilityTypeForSuggest(ResponsibilityTypeCondition condition) {
    return responsibilityTypeDao.selectResponsibilityTypeForSuggest(condition);
  }

}
